﻿/*************************************************************************
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using log4net;
using System.Xml.Linq;

namespace CatchUpTv.Downloader.Australia.AbcIView
{
    /// <summary>
    /// The iView configuration details.
    /// </summary>
    public class Configuration
    {
        /// <summary>
        /// The logger for this class.
        /// </summary>
        static ILog log = LogManager.GetLogger(typeof(Configuration));

        /// <summary>
        /// The API url.
        /// </summary>
        public string ApiUrl { get; set; }

        /// <summary>
        /// The authentication URL.
        /// </summary>
        public string AuthenticationUrl { get; set; }

        /// <summary>
        /// The channels URL.
        /// </summary>
        public string ChannelsUrl { get; set; }

        /// <summary>
        /// Loads the iView configuration.
        /// </summary>
        public Configuration()
        {
            WebClient webClient = new WebClient();

            // Get the iView configuration details
            var rawConfigXml = webClient.DownloadString(AbcIViewDownloader.BaseUrl + "xml/config.xml");

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Configuration XML: '{0}'", rawConfigXml);
            }

            var configXml = XDocument.Parse(rawConfigXml);

            ApiUrl = configXml
                .Descendants("param")
                .Where(e => e.Attribute("name") != null && e.Attribute("name").Value == "api")
                .Select(e => e.Attribute("value").Value)
                .First();

            AuthenticationUrl = configXml
                .Descendants("param")
                .Where(e => e.Attribute("name") != null && e.Attribute("name").Value == "auth")
                .Select(e => e.Attribute("value").Value)
                .First();

            ChannelsUrl = configXml
                .Descendants("param")
                .Where(e => e.Attribute("name") != null && e.Attribute("name").Value == "categories")
                .Select(e => e.Attribute("value").Value)
                .First();

            log.InfoFormat("API URL: '{0}'", ApiUrl);
            log.InfoFormat("Authentication URL: '{0}'", AuthenticationUrl);
            log.InfoFormat("Channels URL: '{0}'", ChannelsUrl);
        }
    }
}
